package com.rr;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
public class FileUploadController {
	
	
	@RequestMapping(value="/upload", method=RequestMethod.POST)  
    public @ResponseBody String handleFileUpload(@RequestParam("name") String name,  
            @RequestParam("file") MultipartFile file){  
        if (!file.isEmpty()) {  
            try {  
                byte[] bytes = file.getBytes();  
                BufferedOutputStream stream =  
                        new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));  
                stream.write(bytes);  
                stream.close();  
                return "You successfully uploaded " + name + " into " + name + "-uploaded !";  
            } catch (Exception e) {  
                return "You failed to upload " + name + " => " + e.getMessage();  
            }  
        } else {  
            return "You failed to upload " + name + " because the file was empty.";  
        }  
    }  
}
